package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.activity.SpringUnionBanquetProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.springunionbanquet.SpringUnionBanquetModule;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.springunionbanquet.SpringUnionBanquetMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;
import java.util.Map;

public class SpringUnionBanquetActivityListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        if(syncType == eUnionSyncType.DeleteUnion.getValue()
            || syncType == eUnionSyncType.BeKick.getValue()
            || syncType == eUnionSyncType.QuitUnion.getValue()
            || syncType == eUnionSyncType.JoinUnion.getValue()){

            long serverId = ConfigHelper.getLong("serverId");
            if(unionInfo.getServerId() == serverId){
                ActivityInfo activityInfo = SpringUnionBanquetMgr.getActivityInfo();
                if(ActivityMgr.activityInTime(activityInfo)) {
                    SpringUnionBanquetProto.CrossSpringUnionMemberSyncMsg.Builder builder = SpringUnionBanquetProto.CrossSpringUnionMemberSyncMsg.newBuilder();
                    builder.setUnionUid(unionInfo.getUnionUid());
                    builder.setActivityId(activityInfo.getActivityId());
                    for (Map.Entry<Long, CrossUnionMember> entry : CrossUnionMgr.getUnionMemberMap(unionInfo.getUnionUid()).entrySet()) {
                        builder.addMember(entry.getKey());
                    }
                    GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_SPRING_UNION_BANQUET_MEMBER_CHANGE, builder));
                }
            }
        }

        if(syncType == eUnionSyncType.JoinUnion.getValue()){
            if(memberList.size() > 0){
                long serverId = ConfigHelper.getLong("serverId");
                CrossUnionMember member = memberList.get(0);
                if(member.getServerId() == serverId){
                    //下发商会的宴请
                    GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                    if(gamePlayer != null){
                        gamePlayer.getModule(SpringUnionBanquetModule.class).afterLogin();
                    }
                }
            }
        }else if(syncType == eUnionSyncType.BeKick.getValue()
                || syncType == eUnionSyncType.QuitUnion.getValue() || syncType == eUnionSyncType.DeleteUnion.getValue()){

            if(memberList.size() > 0){
                long serverId = ConfigHelper.getLong("serverId");
                CrossUnionMember member = memberList.get(0);
                if(member.getServerId() == serverId){
                    //离开商会，清空玩家列表
                    GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                    if(gamePlayer != null){
                        SpringUnionBanquetProto.SpringUnionBanquetInfoSyncMsg.Builder clientMsg = SpringUnionBanquetProto.SpringUnionBanquetInfoSyncMsg.newBuilder();
                        gamePlayer.sendPacket(Protocol.U_SPRING_UNION_BANQUET_SYNC, clientMsg);
                    }
                }
            }

        }

    }
}
